/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：ConvertUtils.java
 * History:
 *       May 24, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 对象类型转换工具类
 * @author liuzhi
 *
 */
public class ConvertUtils {

	/**
	 * 将java bean对象转换为hashmap对象
	 * @author liuzhi
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static HashMap<String, Object> objToHashMap(Object obj) {
	    HashMap<String, Object> hashMap = new HashMap<String, Object>();
	    Class clazz = obj.getClass();
	    List<Class> clazzs = new ArrayList<Class>();

	    do {
	        clazzs.add(clazz);
	        clazz = clazz.getSuperclass();

	    } while (!clazz.equals(Object.class));

	    for (Class cl : clazzs) {
	        Field[] fields = cl.getDeclaredFields();
	        for (Field field : fields) {
	            field.setAccessible(true);
	            try {
					hashMap.put(field.getName(), field.get(obj));
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
	        }
	    }

	    return hashMap;
	}
	
	public static void main(String[] args) {
		
		String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		StringBuilder sbr = new StringBuilder();
		for (int i = 0; i < 10; i++) {
			if(i <= 2){
				sbr.append(chars.charAt((int)(Math.random() * 26)));
			}else{
				sbr.append((int)(Math.random() * 10));
			}
		}
		
		System.out.println(sbr.toString());
	}
}
